import { http } from '@/lib/request'
import type {
  CommonResponse,
  HotTopicItem,
  LabelValue,
  AreaList,
  ToneStore,
  CloseToneStore,
  queryPagination,
} from '../types'

type VideoCreationConfig = {
  language: LabelValue[]
  character: LabelValue[]
}

export type GraphicCreationConfig = {
  pictureType: LabelValue[]
  pictureStyle: LabelValue[]
  officalAccountOrientation: LabelValue[]
}

export const videoService = {
  // 获取系统配置
  getSystemConfig: async () => {
    return http.get<VideoCreationConfig>('/system/config?sign=videoCreation')
  },
  // 获取图形创建配置
  getGraphicCreationConfig: async () => {
    return http.get<GraphicCreationConfig>(`/system/config?sign=graphicCreation`)
  },
  // 热点区域列表
  getHotTopicAreaList: async () => {
    return http.get<AreaList[]>('/hotTopic/areaList')
  },
  // 区域热点前十列表
  /**
   *
   * @param id 区域别名
   * @returns
   */
  getHotTopicList: async (id: string) => {
    return http.get<HotTopicItem[]>('/hotTopic/list', {
      params: {
        id: id,
      },
    })
  },
  // 获取可用音色列表
  getToneList: async () => {
    return http.get<ToneStore[]>('/tone/list')
  },

  // 声音克隆
  createToneStore: async (names: { zh: string; en: string }, url: string) => {
    return http.post<CloseToneStore>('/tone/store', {
      names,
      url,
    })
  },

  // 二次编辑生成的口播文案(扩写 | 改写 | 续写 | 精简)
  editResult: async (data: {
    id: number
    type: string // 扩写 | 改写 | 续写 | 精简
    select_content: string
    original_content: string
  }) => {
    return http.post<{
      result: {
        data: string
      }
    }>('/videoCreationBroadcastCopy/editResult', data)
  },
  // 二次编辑生成的文案(扩写 | 改写 | 续写 | 精简)
  editGraphicResult: async (data: {
    id: number
    type: string // 扩写 | 改写 | 续写 | 精简
    select_content: string
    original_content: string
  }) => {
    return http.post<{
      result: {
        data: string
      }
    }>('/graphicCreation/officalAccount/text/editResult', data)
  },
  // 生成口播文案
  generateValue: async (data = {}) => {
    return http.post<CommonResponse>('/videoCreationBroadcastCopy/store', data)
  },
  // 生成口播文案任务详情
  getBroadcastDetail: async (id: string) => {
    return http.get<{
      status: 'pending' | 'finish' | 'failed'
      result: {
        data: string
        msg?: string
      }
    }>(`/videoCreationBroadcastCopy/detail?id=${id}`)
  },
  // 重新生成口播文案任务
  reloadBroadcastCopy: async (id: string) => {
    return http.get<{
      status: 'pending' | 'finish' | 'failed'
      result: {
        data: string
        msg?: string
      }
    }>(`/videoCreationBroadcastCopy/reload?id=${id}`)
  },

  // 视频创作详情
  videoCreationDetail: async (id: string) => {
    return http.get<{
      status: 'pending' | 'finish' | 'failed'
      result: {
        data: string
      }
    }>(`/videoCreation/detail?no=${id}`)
  },

  // 生成口播文案任务列表
  getBroadcastList: async (
    params: {
      id?: number
    } & queryPagination,
  ) => {
    return http.get<
      {
        result: {
          data: string
        }
        created_at: string
        updated_at: string
      }[]
    >(
      `/videoCreationBroadcastCopy/list?no=${params.id || ''}&pageNum=${params.pageNum}&pageSize=${params.pageSize}&status=finish`,
    )
  },
  // 生成口播视频
  videoCreationAudioVideo: async ({
    broadcast_copy_history_id,
    build_caption,
  }: {
    broadcast_copy_history_id: number
    build_caption: 0 | 1
  }) => {
    return http.post<{
      audioVideo: {
        id: string
        status: 'pending' | 'finish' | 'failed'
      }
    }>('/videoCreationAudioVideo/store', {
      broadcast_copy_history_id,
      build_caption,
    })
  },
  // 生成口播视频任务详情
  getAudioVideoDetail: async (id: string) => {
    return http.get<{
      id: string
      status: 'pending' | 'finish' | 'failed'
      result: {
        msg: string // 文字状态
        success_data: {
          url: ''
        }
      }
    }>(`/videoCreationAudioVideo/detail?id=${id}`)
  },
  // 保存生成的口播文案内容
  updateEditText: async (data: {
    id: string
    content: string
    face_image_url: string // 面部图像
    video_url: string // 原始视频
    vol: string | number
    speed: string
    tone_id: string
  }) => {
    return http.post<any>('/videoCreationBroadcastCopy/saveResult', data)
  },
  // 保存生成的文案内容
  generateGraphicValue: async (data: { id: string; content: string }) => {
    return http.post<CommonResponse>(
      '/graphicCreation/officalAccount/text/saveResult',
      data,
    )
  },
  // 保存生成的口播文案内容
  getAudioVideoHistoryList: async ({
    id,
    pageNum,
    pageSize,
  }: {
    id: string
  } & queryPagination) => {
    return http.get<{
      total: number
      data: {
        id: string
        created_at: string
        updated_at: string
        result?: {
          target_image_face_detect_data?: {
            img_url: string
          }
          success_data?: {
            url?: string
          }
        }
        broadcast_copy_history?: {
          result: {
            data: string
          }
        }
      }[]
    }>(`/videoCreationAudioVideo/list`, {
      params: {
        status: 'finish',
        broadcast_copy_history_id: id,
        pageNum,
        pageSize,
      },
    })
  },
}
